package org.farm2.test.utils;

import lombok.extern.slf4j.Slf4j;
import org.farm2.base.jwt.FarmJwtClaims;
import org.farm2.base.jwt.FarmJwtConfig;
import org.farm2.base.jwt.FarmJwtUtils;
import org.farm2.tools.json.FarmJsons;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.farm2.base.jwt.FarmJwtUtils.getJwtRemainingTime;


@ExtendWith(SpringExtension.class) // 启用 Spring TestContext 框架
@Slf4j
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class JwtTester {
    private static String token=null;

    @Test
    @Order(1)
    void creatToken() {
        FarmJwtUtils.setKeyTypeRandom(false);
        {
            Map<String, Object> claims = new HashMap<>();
            claims.put(FarmJwtConfig.ClaimsKey.LoginName.getKey(), "sysadmin");
            token = FarmJwtUtils.createJwt(FarmJwtConfig.getAuthTtlMillis(), claims);
            System.out.println(token);
        }
        assertThat(token).isNotNull();
    }

    @Test
    @Order(2)
    void showToken() {
        try {
            FarmJwtUtils.setKeyTypeRandom(false);
            FarmJwtClaims claims = FarmJwtUtils.parseJWT(token);
            System.out.println(FarmJsons.toFormatJson(claims));
            System.out.println(getJwtRemainingTime(token));
        } catch (Exception e) {

        }
        assertThat(token).isNotNull();
    }
}
